package com.zmykj.hik.web;

import com.zmykj.hik.common.AjaxResult;
import com.zmykj.hik.common.StringUtils;
import com.zmykj.hik.hikService.Common.HikUtils;
import com.zmykj.hik.hikService.Controllers.HikConfig;
import com.zmykj.hik.hikService.Controllers.MainController;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LengChen
 * @version 1.0
 * @date 2021/5/25
 */
@RestController
@RequestMapping("/api")
public class ApiController {

    /**
     * 下发人员信息
     * @param userList
     * @return
     */
    @PostMapping("/addUserList")
    public AjaxResult addUserList(@RequestBody List<Map<String,String>> userList)
    {
        if(userList.size()>0){
            long lUserID = HikUtils.checkDeviceID(userList.get(0).get("deviceID"));
            if(lUserID == -1){
                return AjaxResult.error("设备离线");
            }
        }
        List<Map<String,String>> errMap = new ArrayList<>();
        for (Map<String,String> map : userList){
            System.out.println(map.toString());
           Boolean addUserResult =  MainController.addUser(map.get("userId"),map.get("userName"),map.get("deviceID"));
            if(!addUserResult){
                errMap.add(map);
            }
        }
        if(errMap.size()>0){
            return AjaxResult.error("失败用户",errMap);
        }
        return AjaxResult.success();
    }


    /**
     * 下发人员验证信息(人脸或指纹)
     * @param userId 用户ID
     * @param userName 用户姓名
     * @param deviceID 设备ID
     * @param fingerprint 指纹ID
     * @param imageUrl 人脸图片URL
     * @return
     */
    @PostMapping("/addUserValidate")
    public AjaxResult addUserFingerprint(String userId,String userName,String deviceID,String fingerprint,String imageUrl)
    {
        imageUrl = imageUrl+"?x-oss-process=image/resize,h_352,m_lfit";
        if(StringUtils.isNotBlank(userId)){
            long lUserID = HikUtils.checkDeviceID(deviceID);
            if(lUserID == -1){
                return AjaxResult.error("设备离线");
            }
        }
        if(StringUtils.isNotBlank(fingerprint)){
            AjaxResult ajaxResult = MainController.downUserFingerprint(userId,deviceID,fingerprint);
            if(!ajaxResult.get(AjaxResult.CODE_TAG).equals("0")){
                return ajaxResult;
            }
        }
        if(StringUtils.isNotBlank(imageUrl)){
             AjaxResult ajaxResult = MainController.downUserFaceImage(userId,userName,deviceID,imageUrl);
             if(!ajaxResult.get(AjaxResult.CODE_TAG).equals("0")){
                 return ajaxResult;
             }
        }
        return AjaxResult.success();
    }


    /**
     * 删除人员信息
     * @param userId 用户ID
     * @param deviceID 设备ID
     * @return
     */
    @PostMapping("/deleteUserInfo")
    public AjaxResult deleteUserInfo(String userId,String deviceID)
    {
        if(StringUtils.isNotBlank(userId)&& StringUtils.isNotBlank(deviceID)){
            long lUserID = HikUtils.checkDeviceID(deviceID);
            if(lUserID == -1){
                return AjaxResult.error("设备离线");
            }
        }else{
            return AjaxResult.error("参数缺失");
        }

        Boolean deleteUserInfo =  MainController.deleateUser(userId,deviceID);

        Boolean deleteFace = MainController.deleteFaceImage(userId,deviceID);

        if(deleteUserInfo && deleteFace){
            return AjaxResult.success();
        }
        return AjaxResult.error("删除失败");
    }


    @PostMapping("/getUserInfo")
    public AjaxResult getUserInfo(String userId,String deviceID)
    {
        if(StringUtils.isNotBlank(userId)&& StringUtils.isNotBlank(deviceID)){
            long lUserID = HikUtils.checkDeviceID(deviceID);
            if(lUserID == -1){
                return AjaxResult.error("设备离线");
            }
        }else{
            return AjaxResult.error("参数缺失");
        }

        Map map = new HashMap();
        map.put("fingerprint",MainController.getUserFingerprint(userId,deviceID));
        String imageUrl = MainController.getUserFaceImageUrl(userId,deviceID);
        map.put("faceImage", imageUrl == null ? null :"http://"+ HikConfig.KMS_IP+":"+HikConfig.KMS_PORT + imageUrl);
        return AjaxResult.success(map);
    }

    /**
     * 删除设备的全部人员信息
     * @param deviceID
     * @return
     */
    @PostMapping("/deleteAllUserInfo")
    public AjaxResult deleteAllUserInfo(String deviceID)
    {
        if(StringUtils.isNotBlank(deviceID)){
            long lUserID = HikUtils.checkDeviceID(deviceID);
            if(lUserID == -1){
                return AjaxResult.error("设备离线");
            }
        }else{
            return AjaxResult.error("参数缺失");
        }

        Boolean deleteUserInfo =  MainController.deleateAllUser(deviceID);

        if(deleteUserInfo){
            return AjaxResult.success();
        }
        return AjaxResult.error("删除失败");
    }

    /**
     * 获取设备联网状态
     * @param deviceID
     * @return
     */
    @RequestMapping(value = "getDeviceStatus")
    public AjaxResult getDeviceStatus(String deviceID)
    {
        if(StringUtils.isNotBlank(deviceID)){
            long lUserID = HikUtils.checkDeviceID(deviceID);
            if(lUserID == -1){
                return AjaxResult.warn("设备离线");
            }else{
                return AjaxResult.success("设备在线");
            }
        }
        return AjaxResult.error("参数缺失");
    }

}
